<script setup lang="ts">
import {
  ContainerStatus,
  ContainerState,
  Container,
  EnvVar
} from "@/views/domain/kubeHud/api/k8s/types";
import KDescGroup from "../../components/KDescGroup.vue";
import KDescRow from "../../components/KDescRow.vue";
import { computed } from "vue";
import InTagLine from "../../components/InTagLine.vue";
import { timeTrans } from "@/views/domain/kubeHud/utils";
import PodVolumesMountList from "./PodVolumesMountList.vue";

type Prop = {
  cState?: ContainerStatus;
  container?: Container;
};

const props = defineProps<Prop>();

const stateType = (state: ContainerState): any => {
  if (state.running) {
    return "success";
  } else if (state.waiting) {
    return "warning";
  } else if (state.terminated) {
    return "danger";
  } else {
    return "info";
  }
};

const stateDesc = (state?: ContainerState): string => {
  if (state?.running) {
    return "Running";
  } else if (state?.waiting) {
    return "Waiting";
  } else if (state?.terminated) {
    return "Terminated";
  } else {
    return "Unknown";
  }
};

const envValDesc = (val: EnvVar): string => {
  if (val.value) {
    return val.value;
  } else if (val.valueFrom?.fieldRef) {
    return val.valueFrom.fieldRef.fieldPath;
  } else if (val.valueFrom?.configMapKeyRef) {
    return val.valueFrom.configMapKeyRef.key;
  } else if (val.valueFrom?.secretKeyRef) {
    return val.valueFrom.secretKeyRef.key;
  } else {
    return "";
  }
};
</script>

<template>
  <KDescGroup>
    <KDescRow
      :label="cState.name"
      :style="{ fontSize: '18px', fontWeight: 'bold' }"
    />
    <KDescRow label="状态">
      <el-tag v-if="cState.state?.running" type="success">Running</el-tag>
      <el-tag v-else-if="cState.state?.waiting" type="warning">
        Waiting
      </el-tag>
      <el-tag v-else-if="cState.state?.terminated" type="danger">
        Terminated
      </el-tag>
      <el-tag v-else type="info">Unknown</el-tag>
    </KDescRow>
    <KDescRow label="镜像" :content="cState.image" />
    <KDescRow label="重启次数" :content="cState.restartCount" />
    <KDescRow label="最近状态">
      <el-space direction="vertical" alignment="start">
        <el-tag :type="stateType(cState.lastState)">
          {{ stateDesc(cState.lastState) }}
        </el-tag>
        <el-space
          v-if="cState.lastState?.terminated"
          direction="vertical"
          alignment="start"
        >
          <InTagLine
            title="Exit Code"
            :content="cState.lastState?.terminated.exitCode"
          />
          <InTagLine
            title="Started"
            :content="timeTrans(cState.lastState?.terminated.startedAt)"
          />
          <InTagLine
            title="Finished"
            :content="timeTrans(cState.lastState?.terminated.finishedAt)"
          />
        </el-space>
        <el-space
          v-else-if="cState.lastState?.running"
          direction="vertical"
          alignment="start"
        >
          <InTagLine
            title="Started"
            :content="timeTrans(cState.lastState?.running.startedAt)"
          />
        </el-space>
        <el-space
          v-else-if="cState.lastState?.waiting"
          direction="vertical"
          alignment="start"
        >
          <InTagLine
            title="Reason"
            :content="cState.lastState?.waiting.reason"
          />

          <InTagLine
            title="Message"
            :content="cState.lastState?.waiting.message"
          />
        </el-space>
      </el-space>
    </KDescRow>
    <KDescRow label="容器ID" :content="cState.containerID" />
    <KDescRow label="镜像拉取策略" :content="container.imagePullPolicy" />
    <KDescRow label="镜像" :content="container.image" />
    <KDescRow label="环境变量">
      <el-space direction="vertical" alignment="start">
        <InTagLine
          v-for="env in container.env"
          :key="env.name"
          :title="env.name"
          :content="envValDesc(env)"
        />
      </el-space>
    </KDescRow>
    <KDescRow label="端口">
      <el-space direction="vertical" alignment="start">
        <InTagLine
          v-for="port in container.ports"
          :key="port.containerPort"
          :title="port.containerPort"
          :content="port.name"
        />
      </el-space>
    </KDescRow>
    <KDescRow label="Liveness Probes">
      <el-space alignment="start">
        <InTagLine
          title="httpGet"
          :content="
            [
              `path: ${container.livenessProbe?.httpGet?.path}`,
              `port: ${container.livenessProbe?.httpGet?.port}`,
              `scheme: ${container.livenessProbe?.httpGet?.scheme}`
            ].join(',')
          "
        />
        <InTagLine
          title="Success"
          :content="container.livenessProbe?.successThreshold"
        />
        <InTagLine
          title="Failure"
          :content="container.livenessProbe?.failureThreshold"
        />
        <InTagLine
          title="Delay"
          :content="container.livenessProbe?.initialDelaySeconds"
        />
        <InTagLine
          title="Timeout"
          :content="container.livenessProbe?.timeoutSeconds"
        />
        <InTagLine
          title="Period"
          :content="container.livenessProbe?.periodSeconds"
        />
      </el-space>
    </KDescRow>
    <KDescRow label="Volume Mounts" vertical>
      <PodVolumesMountList :volumes="container.volumeMounts" />
    </KDescRow>
  </KDescGroup>
</template>
<style lang="scss" scoped></style>
